package com.gesee.zyspps.activity;

import java.util.ArrayList;
import java.util.List;

import com.gesee.zys.R;
import com.gesee.zyspps.ZSApplication;
import com.gesee.zyspps.db.CmsFeature;
import com.gesee.zyspps.db.CmsPipelineField;
import com.gesee.zyspps.db.CmsPointsName;
import com.gesee.zyspps.db.CmsPointsNameDao.Properties;
import com.gesee.zyspps.utils.StringUtil;
import com.gesee.zyspps.utils.ToastUtil;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

public class UpdateTeZhengActivity extends BaseActivity
{

    private Spinner subCodeSpin,filedNameSpinner;
    private Button enter, cancle;
    private ImageView come_back;
    private EditText et_subCode,filedValue,encoded,alias,code,remark;
    private String filedNameValue ;
    private CmsFeature oldCms;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_tezheng);
        oldCms = (CmsFeature) getIntent().getSerializableExtra("CmsFeature");
        initView();
    }

    private void initView()
    {
        // TODO Auto-generated method stub
        et_subCode = (EditText) findViewById(R.id.et_subCode);
        et_subCode.setText(oldCms.getSubCodes());
        filedValue = (EditText) findViewById(R.id.filedValue);
        filedValue.setText(oldCms.getFiledValue());
        encoded = (EditText) findViewById(R.id.encoded);
        encoded.setText(oldCms.getEncoded());
        alias = (EditText) findViewById(R.id.alias);
        alias.setText(oldCms.getAlias());
        code = (EditText) findViewById(R.id.code);
        code.setText(oldCms.getCode());
        remark = (EditText) findViewById(R.id.remark);
        remark.setText(oldCms.getRemark());
        
        come_back = (ImageView) findViewById(R.id.come_back);
        come_back.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                UpdateTeZhengActivity.this.finish();
            }
        });
        
        
        
        enter = (Button) findViewById(R.id.enter);
        cancle = (Button) findViewById(R.id.cancle);
        
        subCodeSpin = (Spinner) findViewById(R.id.subCodeSpin);
        List<CmsPointsName> list = ZSApplication.getDaoSession("zyspps-db").getCmsPointsNameDao().queryBuilder().orderAsc(Properties.Sort).list();
        List cmsPointList = new ArrayList();
        cmsPointList.add("ALL");
        if(list!=null&&list.size()>0){
            for(int i = 0;i<list.size();i++){
                cmsPointList.add(list.get(i).getSubCode());
            }
        }
        ArrayAdapter subCodeAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, cmsPointList);
        subCodeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        subCodeSpin.setAdapter(subCodeAdapter);
        for(int i = 0;i<cmsPointList.size();i++){
            if(cmsPointList.get(i).equals(oldCms.getSubCodes())){
                subCodeSpin.setSelection(i);
            }
        }
        subCodeSpin.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> parent, View arg1, int position, long arg3)
            {
                // TODO Auto-generated method stub
                et_subCode.setText(parent.getItemAtPosition(position).toString());
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub
                
            }
        });
        
        
        filedNameSpinner = (Spinner) findViewById(R.id.filedName);
        List<CmsPipelineField> allPipelineList = ZSApplication.getDaoSession("zyspps-db").getCmsPipelineFieldDao().queryBuilder().orderAsc(com.gesee.zyspps.db.CmsPipelineFieldDao.Properties.Sort).list();
        List cmsPipeLineList = new ArrayList();
        if(allPipelineList!=null&&allPipelineList.size()>0){
            for(int i = 0;i<allPipelineList.size();i++){
                cmsPipeLineList.add(allPipelineList.get(i).getFiledName());
            }
        }
        ArrayAdapter filedNameAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, cmsPipeLineList);
        filedNameAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        filedNameSpinner.setAdapter(filedNameAdapter);
        //if(oldCms.getFiledName().equals(other));
        for(int i = 0;i<cmsPipeLineList.size();i++){
            if(cmsPipeLineList.get(i).equals(oldCms.getFiledName())){
                filedNameSpinner.setSelection(i);
            }
        }
        filedNameSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> parent, View arg1, int position, long arg3)
            {
                // TODO Auto-generated method stub
                filedNameValue = parent.getItemAtPosition(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub
                
            }
        });
        /*xianshi = (Spinner) findViewById(R.id.xianshi);
        List xianshiList = new ArrayList();
        xianshiList.add("ALL");
        ArrayAdapter xianshiAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, xianshiList);
        xianshi.setAdapter(xianshiAdapter);
        
        xianshi.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3)
            {
                // TODO Auto-generated method stub
                xianshiValue = "ALL";
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub
                
            }
        });*/
        
        enter.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub ,encoded,alias,code,remark
                String subCodes = et_subCode.getText().toString().trim();
                String filedTeZhengValue = filedValue.getText().toString().trim();
                String aliasValue = alias.getText().toString().trim();
                String codeValue = code.getText().toString().trim();
                String remarkValue = remark.getText().toString().trim();
                String encodedValue = encoded.getText().toString().trim();
                
               // List<CmsFeature> list = ZSApplication.getDaoSession("zyspps-db").getCmsFeatureDao().queryBuilder().orderDesc(com.gesee.zyspps.db.CmsFeatureDao.Properties.Sort).list();
                
                //CmsFeature cms = new CmsFeature();
                oldCms.setAlias(aliasValue);
                oldCms.setCode(codeValue);
                oldCms.setSubCodes(subCodes);
                oldCms.setEncoded(encodedValue);
                oldCms.setFiledName(filedNameValue);
                oldCms.setFiledValue(filedTeZhengValue);
                oldCms.setRemark(remarkValue);
                /*if(list!=null&&list.size()>0){
                    cms.setSort(list.get(0).getSort()+1);
                }else{
                    cms.setSort(1);
                }*/
                if(StringUtil.isEmpty(subCodes)||StringUtil.isEmpty(filedTeZhengValue)){
                    ToastUtil.show(UpdateTeZhengActivity.this, "字段名称必须填写!");
                }else{
                    ZSApplication.getDaoSession("zyspps-db").getCmsFeatureDao().update(oldCms);
                    UpdateTeZhengActivity.this.finish();
                }
            }
        });
        
        cancle.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                UpdateTeZhengActivity.this.finish();
            }
        });

    }
}
